package GUILayer;

import java.awt.EventQueue;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JTabbedPane;
import javax.swing.JDesktopPane;
import javax.swing.JSplitPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JLabel;

import ControlLayer.*;
import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;

import javax.swing.SwingConstants;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.border.CompoundBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;

import java.awt.Container;
import java.awt.TextArea;
import java.awt.ScrollPane;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Panel;
import java.awt.Choice;
import javax.swing.JComboBox;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.TextField;

import java.lang.Integer;

public class MainMenu {

	private JFrame frmWesternStyle;
	public SalesControl SalesCtr;
	public PersonControl PersonCtr;
	public ProductControl ProductCtr;
	public Person person;
	private JFormattedTextField frmtdtxtfldIndtastProduktid;
	private JFormattedTextField frmtdtxtfldProduktid_1;
	private JFormattedTextField formattedTextField;
	private JFormattedTextField formattedTextField_1;
	private JFormattedTextField formattedTextField_2;
	private JFormattedTextField formattedTextField_3;
	private JFormattedTextField formattedTextField_4;
	private JFormattedTextField formattedTextField_5;
	private JFormattedTextField formattedTextField_6;
	private JFormattedTextField formattedTextField_7;
	private JFormattedTextField formattedTextField_8;
	private JFormattedTextField formattedTextField_9;
	private JFormattedTextField formattedTextField_10;
	private JFormattedTextField formattedTextField_11;
	private JFormattedTextField frmtdtxtfldProduktid;
	private JFormattedTextField frmtdtxtfldVareNavn;
	private JFormattedTextField frmtdtxtfldIndkbspris;
	private JFormattedTextField frmtdtxtfldSalgspris;
	private JFormattedTextField frmtdtxtfldPrisForLeje;
	private JFormattedTextField frmtdtxtfldNationalitet;
	private JFormattedTextField frmtdtxtfldMinLager;
	private JFormattedTextField frmtdtxtfldBeskrivelsesType;
	private JFormattedTextField frmtdtxtfldStrrelse;
	private JFormattedTextField frmtdtxtfldFarve;
	private JFormattedTextField frmtdtxtfldBeskrivelse;
	private JFormattedTextField frmtdtxtfldFabrikat;
	private JFormattedTextField frmtdtxtfldKaliber;
	private JComboBox comboBox;
	private JTextPane txtpnAntal;
	private JTextPane textPane_1;
	private JTextPane textPaneKunde;
	private JTextPane txtpnIndtastSgeord;
	private TextField textField;
	private TextArea textArea;
	private String kvittteringTekst;
	private String kvitteringTekst;
	private ArrayList<OrderLine> orderline;
	private String hej;
	private JTextPane textPane;
	private String Totalpris1;
	private JTabbedPane tabbedPane;
	private JTabbedPane tabbedPane_1;
	private int pris;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainMenu window = new MainMenu();
					window.frmWesternStyle.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * Initilizes SalesMenu, OrderMenu og EditOrderMenu
	 */
	public MainMenu() {
		initialize();
		initSalgMenu();
		initOpretVare();
		initRedigerMenu();
		SalesCtr = new SalesControl();
		ProductCtr = new ProductControl();
		PersonCtr = new PersonControl();
		orderline = new ArrayList<OrderLine>();
		Totalpris1 = "";
		pris = 0;
	}
	
	/*
	 * Method to add an object Orderline to an ArrayList
	 */
	public void addSaleLine(OrderLine o)
	{
		orderline.add(o);
	}
	/*
	 * Method to delete an object Orderline from an ArrayList
	 */
	
	public void deleteSaleLine(OrderLine o)
	{
		orderline.remove(o);
	}
	/*
	 * Method to show the objects in the ArrayList
	 */
	
	public ArrayList<OrderLine> getorderline()
	{
		return orderline;
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmWesternStyle = new JFrame();
		frmWesternStyle.setTitle("Western Style");
		frmWesternStyle.setBounds(100, 100, 746, 632);
		frmWesternStyle.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmWesternStyle.getContentPane().setLayout(null);

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 27, 738, 578);
		frmWesternStyle.getContentPane().add(tabbedPane);

	}
	
	/**
	 * Method that initializes the SalesMenu
	 */
	private void initSalgMenu()
	{
		JPanel panel = new JPanel();
		tabbedPane.addTab("Salg", null, panel, null);
		panel.setLayout(null);

		JLabel lblOpretSalg = new JLabel("Opret Salg:");
		lblOpretSalg.setBounds(10, 11, 87, 14);
		panel.add(lblOpretSalg);

		JLabel lblIndtastTelefonnummer = new JLabel("Indtast KundeID");
		lblIndtastTelefonnummer.setBounds(10, 36, 104, 20);
		panel.add(lblIndtastTelefonnummer);

		JLabel lblTilfjfjernVare = new JLabel("Tilf\u00F8j/Fjern Vare:");
		lblTilfjfjernVare.setBounds(10, 68, 142, 14);
		panel.add(lblTilfjfjernVare);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 260, 473, 282);
		panel.add(scrollPane);
		
				textArea = new TextArea();
				scrollPane.setViewportView(textArea);
				
		/**
		 * method to add an Object to the ArrayList<OrderLine>
		 * and print out as an recit in a textArea
		 */

		JButton btnSg_1 = new JButton("Tilf\u00F8j");
		btnSg_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				
				
				
				Product pro = ProductCtr.findProductByPid(Integer.parseInt(txtpnIndtastSgeord.getText()));
				OrderLine ord = new OrderLine();
				ord.setProduct(pro);
				ord.setPcs(Integer.parseInt(txtpnAntal.getText()));
				addSaleLine(ord);
				kvittteringTekst = "";
				int Totalpris = 0;
				for(OrderLine o : getorderline())
				{
					Totalpris = (o.getPcs() * o.getProduct().getSalesPrice());
					kvittteringTekst += " \n ***************************************\nProduktNavn:		 " 
				+ o.getProduct().getName() +  " \nAntal:			 " + o.getPcs() +" \nSalgsPris:		 " + o.getProduct().getSalesPrice() + "\n\n\nPris:			 " + (o.getPcs() * o.getProduct().getSalesPrice()+ " \n ***************************************" );
					
				}
					pris += Totalpris;
					textPane.setText(Integer.toString(pris));
					textArea.setText(kvittteringTekst);
			}
		});
		btnSg_1.setBounds(247, 178, 91, 23);
		panel.add(btnSg_1);
		
		

		txtpnAntal = new JTextPane();
		txtpnAntal.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				txtpnAntal.setText("");
			}
		});
		txtpnAntal.setText("Antal");
		txtpnAntal.setBounds(247, 147, 87, 20);
		panel.add(txtpnAntal);

		/**
		 * Method to remove an Object from the ArrayList<Orderline>
		 * and remove the text from the recit textArea
		 */
		
		JButton btnFjern = new JButton("Fjern");
		btnFjern.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				Product pro = ProductCtr.findProductByPid(Integer.parseInt(txtpnIndtastSgeord.getText()));
				
				int index = 0;
				boolean found = false;
				while(index < orderline.size() && !found)
				{
					OrderLine ord = orderline.get(index);
					if(ord.getProduct().getName().equals(pro.getName()))
					{
						found = true;
						deleteSaleLine(ord);
					}
					index++;
				}
				
				kvittteringTekst = "";
				int Totalpris = 0;
				pris = 0;
				for(OrderLine o : getorderline())
				{
					Totalpris += (o.getPcs() * o.getProduct().getSalesPrice());
					kvittteringTekst += " \n ***************************************\nProduktNavn:		 " 
				+ o.getProduct().getName() +  " \nAntal:			 " + o.getPcs() +" \nSalgsPris:		 " + o.getProduct().getSalesPrice() + "\n\n\nPris:		 " + (o.getPcs() * o.getProduct().getSalesPrice()+ " \n ***************************************" );
					pris += Totalpris;
				}
					textPane.setText(Integer.toString(pris));
					textArea.setText(kvittteringTekst);

				
			}
		});
		btnFjern.setBounds(348, 178, 91, 23);
		panel.add(btnFjern);
		
		/**
		 * Method to create a new Sale 
		 * and add the Object From the ArrayList<OrderLine>
		 * to the new Sale
		 */

		JButton btnGemOgUdskriv = new JButton("Gem og udskriv ordren");
		btnGemOgUdskriv.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				try
				{
					Person c = null;
				SalesOrder s = null;
				System.out.println("hello1");
				String he = textPaneKunde.getText();
				if(he.length() < 1)
				{
					c = null;
				}
				else
				{
					c = PersonCtr.findCustomer(Integer.parseInt(textPaneKunde.getText()));
				}
				if(c == null)
				{
					System.out.println("hello2");
					s = SalesCtr.createNewSale(PersonCtr.findCustomer(2).getId());
					System.out.println("hello3");
				}
				else
				{
					System.out.println("hello4");
					s = SalesCtr.createNewSale(c.getId());
					System.out.println("hello5");
				}
				
					for(OrderLine ord : orderline)
					{
						SalesCtr.addProductToSalesOrder(s.getsId(), ord.getProduct().getpId() , ord.getPcs());
					}
					textArea.setText("");
					txtpnIndtastSgeord.setText("Indtast ProduktID");
					txtpnAntal.setText("Antal");
					textField.setText("");
					
				}
				catch(NumberFormatException i)
				{
					System.out.print(i);
				}
			
			}
		});
		btnGemOgUdskriv.setBounds(510, 491, 213, 51);
		panel.add(btnGemOgUdskriv);

		textPaneKunde = new JTextPane();
		textPaneKunde.setBounds(114, 36, 91, 20);
		panel.add(textPaneKunde);
		
		/**
		 * Method to find an old sale
		 */

		JButton btnSg = new JButton("S\u00F8g");
		btnSg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				textArea.setText("");
				SalesOrder sale = SalesCtr.findSalesOrder(Integer.parseInt(textPane_1.getText()));				
				kvitteringTekst = "Kunde: " + sale.getCustomer() + "\nDato: " + sale.getDate() + " \nLeveringsDato: " + sale.getDeliveryDate() + "\nTotal Pris: " + sale.getAmount() + "\nLeverings Status: " + sale.getDeliveryStatus();
				
				textArea.setText(kvitteringTekst);				
			}
		});
		btnSg.setBounds(632, 64, 91, 23);
		panel.add(btnSg);

		JLabel lblRedigererOrdre = new JLabel("Redigerer Ordre:");
		lblRedigererOrdre.setBounds(391, 0, 142, 36);
		panel.add(lblRedigererOrdre);

		textPane_1 = new JTextPane();
		textPane_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				textPane_1.setText("");
			}
		});
		textPane_1.setBounds(480, 65, 142, 20);
		panel.add(textPane_1);

		JLabel lblIndtastSalgsid = new JLabel("Indtast SalgsID:");
		lblIndtastSalgsid.setBounds(391, 69, 91, 14);
		panel.add(lblIndtastSalgsid);

		JButton btnUdskrivFaktura = new JButton("Udskriv Faktura");
		btnUdskrivFaktura.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
			}
		});
		btnUdskrivFaktura.setBounds(541, 160, 182, 23);
		panel.add(btnUdskrivFaktura);
		
		/**
		 * Method to find a Customer Object
		 */

		JButton btnSg_2 = new JButton("S\u00F8g");
		btnSg_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					System.out.println("hej1");
					if(PersonCtr.findCustomer(Integer.parseInt(textPaneKunde.getText())) != null)
					{
						textField.setText("Kunden er fundet");
					}
					else
					{
						textField.setText("Kunden findes ikke i databasen.");
					}
				} catch (NumberFormatException i) {

				}

			}
		});
		btnSg_2.setBounds(225, 35, 91, 23);
		panel.add(btnSg_2);

		txtpnIndtastSgeord = new JTextPane();
		txtpnIndtastSgeord.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				txtpnIndtastSgeord.setText("");
			}
		});
		txtpnIndtastSgeord.setText("Indtast ProduktID");
		txtpnIndtastSgeord.setBounds(10, 93, 112, 20);
		panel.add(txtpnIndtastSgeord);
		
		/**
		 * Method to find a product
		 * by its ID
		 */

		JButton btnSg_3 = new JButton("S\u00F8g");
		btnSg_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textField.setText("");
				
				
				Product eq = ProductCtr.findProductByPid(Integer.parseInt(txtpnIndtastSgeord
						.getText()));
				
				if(eq != null)
				{
					textField.setText(eq.getDescription());
				}

				
				
			}

		});
		btnSg_3.setBounds(132, 93, 91, 23);
		panel.add(btnSg_3);

		textField = new TextField();
		textField.setBounds(10, 119, 213, 116);
		panel.add(textField);
				
				JLabel lblTotalpris = new JLabel("Totalpris:");
				lblTotalpris.setBounds(510, 386, 62, 14);
				panel.add(lblTotalpris);
				
				textPane = new JTextPane();
				textPane.setBounds(581, 380, 142, 20);
				panel.add(textPane);

	}
	private void initOpretVare()
	{
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Varer", null, panel_1, null);
		panel_1.setLayout(null);

		JTabbedPane tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane_1.setBounds(0, 0, 733, 553);
		panel_1.add(tabbedPane_1);

		Panel panel_2 = new Panel();
		tabbedPane_1.addTab("Opret Vare\r\n", null, panel_2, null);
		panel_2.setLayout(null);

		JLabel lblVlgVaretype = new JLabel("V\u00E6lg VareType:");
		lblVlgVaretype.setBounds(10, 11, 137, 14);
		panel_2.add(lblVlgVaretype);

		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Equipment",
				"Clothing", "GunReplicas" }));
		comboBox.setBounds(194, 11, 137, 22);
		panel_2.add(comboBox);

		frmtdtxtfldProduktid = new JFormattedTextField();
		frmtdtxtfldProduktid.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				frmtdtxtfldProduktid.setText("");

			}
		});
		frmtdtxtfldProduktid.setText("ProduktID:");
		frmtdtxtfldProduktid.setBounds(10, 102, 137, 20);
		panel_2.add(frmtdtxtfldProduktid);

		frmtdtxtfldVareNavn = new JFormattedTextField();
		frmtdtxtfldVareNavn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				frmtdtxtfldVareNavn.setText("");
			}
		});

		frmtdtxtfldVareNavn.setText("Vare Navn:\r\n");
		frmtdtxtfldVareNavn.setBounds(10, 133, 137, 20);
		panel_2.add(frmtdtxtfldVareNavn);

		frmtdtxtfldIndkbspris = new JFormattedTextField();
		frmtdtxtfldIndkbspris.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldIndkbspris.setText("");
			}
		});

		frmtdtxtfldIndkbspris.setText("Indk\u00F8bsPris:");
		frmtdtxtfldIndkbspris.setBounds(10, 164, 137, 20);
		panel_2.add(frmtdtxtfldIndkbspris);

		frmtdtxtfldSalgspris = new JFormattedTextField();
		frmtdtxtfldSalgspris.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldSalgspris.setText("");
			}
		});

		frmtdtxtfldSalgspris.setText("SalgsPris:\r\n");
		frmtdtxtfldSalgspris.setBounds(10, 195, 137, 20);
		panel_2.add(frmtdtxtfldSalgspris);

		frmtdtxtfldPrisForLeje = new JFormattedTextField();
		frmtdtxtfldPrisForLeje.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldPrisForLeje.setText("");
			}
		});

		frmtdtxtfldPrisForLeje.setText("Pris for leje:");
		frmtdtxtfldPrisForLeje.setBounds(10, 226, 137, 20);
		panel_2.add(frmtdtxtfldPrisForLeje);

		frmtdtxtfldNationalitet = new JFormattedTextField();
		frmtdtxtfldNationalitet.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldNationalitet.setText("");
			}
		});

		frmtdtxtfldNationalitet.setText("Nationalitet:");
		frmtdtxtfldNationalitet.setBounds(10, 257, 137, 20);
		panel_2.add(frmtdtxtfldNationalitet);

		frmtdtxtfldMinLager = new JFormattedTextField();
		frmtdtxtfldMinLager.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldMinLager.setText("");
			}
		});

		frmtdtxtfldMinLager.setText("Min Lager:");
		frmtdtxtfldMinLager.setBounds(10, 288, 137, 20);
		panel_2.add(frmtdtxtfldMinLager);

		frmtdtxtfldBeskrivelsesType = new JFormattedTextField();
		frmtdtxtfldBeskrivelsesType.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldBeskrivelsesType.setText("");
			}
		});

		frmtdtxtfldBeskrivelsesType.setText("Beskrivelses Type:");
		frmtdtxtfldBeskrivelsesType.setBounds(285, 102, 137, 20);
		panel_2.add(frmtdtxtfldBeskrivelsesType);

		frmtdtxtfldStrrelse = new JFormattedTextField();
		frmtdtxtfldStrrelse.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldStrrelse.setText("");
			}
		});

		frmtdtxtfldStrrelse.setText("St\u00F8rrelse:");
		frmtdtxtfldStrrelse.setBounds(285, 133, 137, 20);
		panel_2.add(frmtdtxtfldStrrelse);

		frmtdtxtfldFarve = new JFormattedTextField();
		frmtdtxtfldFarve.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldFarve.setText("");
			}
		});

		frmtdtxtfldFarve.setText("Farve:");
		frmtdtxtfldFarve.setBounds(285, 164, 137, 20);
		panel_2.add(frmtdtxtfldFarve);

		frmtdtxtfldBeskrivelse = new JFormattedTextField();
		frmtdtxtfldBeskrivelse.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldBeskrivelse.setText("");
			}
		});

		frmtdtxtfldBeskrivelse.setText("Beskrivelse:");
		frmtdtxtfldBeskrivelse.setBounds(285, 195, 137, 20);
		panel_2.add(frmtdtxtfldBeskrivelse);

		frmtdtxtfldFabrikat = new JFormattedTextField();
		frmtdtxtfldFabrikat.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldFabrikat.setText("");
			}
		});

		frmtdtxtfldFabrikat.setText("Fabrikat:");
		frmtdtxtfldFabrikat.setBounds(285, 226, 137, 20);
		panel_2.add(frmtdtxtfldFabrikat);

		frmtdtxtfldKaliber = new JFormattedTextField();
		frmtdtxtfldKaliber.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frmtdtxtfldKaliber.setText("");
			}
		});

		frmtdtxtfldKaliber.setText("Kaliber:");
		frmtdtxtfldKaliber.setBounds(285, 257, 137, 20);
		panel_2.add(frmtdtxtfldKaliber);
		
		/**
		 * Method to create a new product 
		 * to the database
		 */

		JButton btnNewButton = new JButton("Opret Vare");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBox.getSelectedIndex() == 0) {
					try {
						ProductCtr.createEquipment(
								Integer.parseInt(frmtdtxtfldProduktid.getText()),
								frmtdtxtfldVareNavn.getText(),
								Integer.parseInt(frmtdtxtfldIndkbspris
										.getText()),
								Integer.parseInt(frmtdtxtfldSalgspris.getText()),
								Integer.parseInt(frmtdtxtfldPrisForLeje
										.getText()),
								frmtdtxtfldNationalitet.getText(),
								Integer.parseInt(frmtdtxtfldMinLager.getText()),
								frmtdtxtfldBeskrivelsesType.getText(),
								frmtdtxtfldBeskrivelse.getText());
						System.out.println("hej1");

						if (e.getSource() != null) {
							frmtdtxtfldProduktid.setText("ProduktID:");
							frmtdtxtfldVareNavn.setText("Vare Navn:");
							frmtdtxtfldIndkbspris.setText("Indk\u00F8bsPris:");
							frmtdtxtfldSalgspris.setText("Salgspris:");
							frmtdtxtfldPrisForLeje.setText("Pris for leje:");
							frmtdtxtfldNationalitet.setText("Nationalitet:");
							frmtdtxtfldMinLager.setText("Min Lager:");
							frmtdtxtfldBeskrivelsesType
									.setText("TypeBeskrivelse");
							frmtdtxtfldBeskrivelse.setText("Beskrivelse:");
						}
					} catch (Exception n) {

					}
				}
			}
		});
		btnNewButton.setBounds(528, 440, 190, 77);
		panel_2.add(btnNewButton);
	}
		

		private void initRedigerMenu()
		{
			Panel panel_3 = new Panel();
			tabbedPane.addTab("Rediger Vare", null, panel_3, null);
			panel_3.setLayout(null);

			frmtdtxtfldProduktid_1 = new JFormattedTextField();
			frmtdtxtfldProduktid_1.setBounds(176, 112, 124, 20);
			panel_3.add(frmtdtxtfldProduktid_1);

			formattedTextField = new JFormattedTextField();
			formattedTextField.setBounds(176, 143, 124, 20);
			panel_3.add(formattedTextField);

			formattedTextField_1 = new JFormattedTextField();
			formattedTextField_1.setBounds(176, 174, 124, 20);
			panel_3.add(formattedTextField_1);

			formattedTextField_2 = new JFormattedTextField();
			formattedTextField_2.setBounds(176, 205, 124, 20);
			panel_3.add(formattedTextField_2);

			formattedTextField_3 = new JFormattedTextField();
			formattedTextField_3.setBounds(176, 236, 124, 20);
			panel_3.add(formattedTextField_3);

			formattedTextField_4 = new JFormattedTextField();
			formattedTextField_4.setBounds(176, 267, 124, 20);
			panel_3.add(formattedTextField_4);

			formattedTextField_5 = new JFormattedTextField();
			formattedTextField_5.setBounds(176, 298, 124, 20);
			panel_3.add(formattedTextField_5);

			formattedTextField_6 = new JFormattedTextField();
			formattedTextField_6.setBounds(497, 112, 124, 20);
			panel_3.add(formattedTextField_6);

			formattedTextField_7 = new JFormattedTextField();
			formattedTextField_7.setBounds(497, 143, 124, 20);
			panel_3.add(formattedTextField_7);

			formattedTextField_8 = new JFormattedTextField();
			formattedTextField_8.setBounds(497, 174, 124, 20);
			panel_3.add(formattedTextField_8);

			formattedTextField_9 = new JFormattedTextField();
			formattedTextField_9.setBounds(497, 205, 124, 20);
			panel_3.add(formattedTextField_9);

			formattedTextField_10 = new JFormattedTextField();
			formattedTextField_10.setBounds(497, 236, 124, 20);
			panel_3.add(formattedTextField_10);

			formattedTextField_11 = new JFormattedTextField();
			formattedTextField_11.setBounds(497, 267, 124, 20);
			panel_3.add(formattedTextField_11);
			
			/**
			 * Method that find an existing product
			 * in the database
			 */

			JButton btnNewButton_1 = new JButton("S\u00F8g");
			btnNewButton_1.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					frmtdtxtfldIndtastProduktid.setText("Indtast ProduktID:");
				}
			});
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String aString = frmtdtxtfldIndtastProduktid.getText();
					int aInt = Integer.parseInt(aString);
					System.out.println("hej1");

					Product p = ProductCtr.findProductByPid(aInt);
					if (p != null) {
						int pID = p.getpId();
						String pI = Integer.toString(pID);
						frmtdtxtfldProduktid_1.setText(pI);

						formattedTextField.setText(p.getName());

						int prise = p.getPurchasePrice();
						String pris = Integer.toString(prise);
						formattedTextField_1.setText(pris);

						int sale = p.getSalesPrice();
						String salg = Integer.toString(sale);
						formattedTextField_2.setText(salg);

						int leje = p.getRentPrice();
						String lejee = Integer.toString(leje);
						formattedTextField_3.setText(lejee);

						formattedTextField_4.setText(p.getCountryOfOrigen());

						int minstock = p.getMinStock();
						String min = Integer.toString(minstock);
						formattedTextField_5.setText(min);

						formattedTextField_6.setText(p.getDescriptionType());

						formattedTextField_7.setText(p.getSize());

						formattedTextField_8.setText(p.getDescription());

						formattedTextField_9.setText(p.getColour());

						formattedTextField_10.setText(p.getFabric());

						formattedTextField_11.setText(p.getCalibre());

					}
				}
			});
			btnNewButton_1.setBounds(176, 13, 91, 23);
			panel_3.add(btnNewButton_1);

			frmtdtxtfldIndtastProduktid = new JFormattedTextField();
			frmtdtxtfldIndtastProduktid.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					frmtdtxtfldIndtastProduktid.setText("");
				}
			});
			frmtdtxtfldIndtastProduktid.setText("Indtast ProduktID:");
			frmtdtxtfldIndtastProduktid.setBounds(29, 14, 118, 20);
			panel_3.add(frmtdtxtfldIndtastProduktid);

			JLabel lblProduktid = new JLabel("ProduktID:");
			lblProduktid.setBounds(29, 115, 137, 14);
			panel_3.add(lblProduktid);

			JLabel lblVareNavn = new JLabel("Vare Navn:");
			lblVareNavn.setBounds(29, 145, 137, 14);
			panel_3.add(lblVareNavn);

			JLabel lblIndkbspris = new JLabel("Indk\u00F8bsPris:");
			lblIndkbspris.setBounds(29, 176, 137, 14);
			panel_3.add(lblIndkbspris);

			JLabel lblSalgspris = new JLabel("SalgsPris:");
			lblSalgspris.setBounds(29, 207, 137, 14);
			panel_3.add(lblSalgspris);

			JLabel lblPrisForLeje = new JLabel("Pris for leje:");
			lblPrisForLeje.setBounds(29, 238, 137, 14);
			panel_3.add(lblPrisForLeje);

			JLabel lblMinLager = new JLabel("Min. lager:");
			lblMinLager.setBounds(29, 304, 137, 14);
			panel_3.add(lblMinLager);

			JLabel lblTypebeskrivelse = new JLabel("TypeBeskrivelse:");
			lblTypebeskrivelse.setBounds(388, 115, 99, 14);
			panel_3.add(lblTypebeskrivelse);

			JLabel lblStrrelse = new JLabel("St\u00F8rrelse:");
			lblStrrelse.setBounds(388, 174, 99, 14);
			panel_3.add(lblStrrelse);

			JLabel lblBeskrivelse = new JLabel("Beskrivelse:");
			lblBeskrivelse.setBounds(388, 205, 99, 14);
			panel_3.add(lblBeskrivelse);

			JLabel lblFabrikat = new JLabel("Fabrikat");
			lblFabrikat.setBounds(388, 236, 99, 14);
			panel_3.add(lblFabrikat);

			JLabel lblKaliber = new JLabel("Kaliber:");
			lblKaliber.setBounds(388, 267, 99, 14);
			panel_3.add(lblKaliber);

			JLabel lblNationalitet = new JLabel("Nationalitet:");
			lblNationalitet.setBounds(29, 273, 137, 14);
			panel_3.add(lblNationalitet);

			JLabel lblFarve = new JLabel("Farve:");
			lblFarve.setBounds(388, 146, 99, 14);
			panel_3.add(lblFarve);
			
			/**
			 * Method to save a Product to
			 * the Database
			 */

			JButton btnNewButton_2 = new JButton("Gem Vare");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						String aString = frmtdtxtfldIndtastProduktid.getText();
						int aInt = Integer.parseInt(aString);
						Product pro = ProductCtr.findProductByPid(aInt);

						pro.setpId(Integer.parseInt(frmtdtxtfldProduktid_1
								.getText()));
						pro.setName(formattedTextField.getText());
						pro.setPurchasePrice(Integer.parseInt(formattedTextField_1
								.getText()));
						pro.setSalesPrice(Integer.parseInt(formattedTextField_2
								.getText()));
						pro.setRentPrice(Integer.parseInt(formattedTextField_3
								.getText()));
						pro.setCountryOfOrigen(formattedTextField_4.getText());
						pro.setMinStock(Integer.parseInt(formattedTextField_5
								.getText()));
						pro.setDescriptionType(formattedTextField_6.getText());
						pro.setSize(formattedTextField_7.getText());
						pro.setDescription(formattedTextField_8.getText());
						pro.setColour(formattedTextField_9.getText());
						pro.setFabric(formattedTextField_10.getText());
						pro.setCalibre(formattedTextField_11.getText());

						frmtdtxtfldProduktid_1.setText("");
						formattedTextField.setText("");
						formattedTextField_1.setText("");
						formattedTextField_2.setText("");
						formattedTextField_3.setText("");
						formattedTextField_4.setText("");
						formattedTextField_5.setText("");
						formattedTextField_6.setText("");
						formattedTextField_7.setText("");
						formattedTextField_8.setText("");
						formattedTextField_9.setText("");
						formattedTextField_10.setText("");
						formattedTextField_11.setText("");
					} catch (NumberFormatException e) {

					} catch (IndexOutOfBoundsException e) {

					}
				}
			});
			btnNewButton_2.setBounds(520, 371, 196, 40);
			panel_3.add(btnNewButton_2);
			
			JButton btnRydFelter = new JButton("Ryd felter");
			btnRydFelter.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					frmtdtxtfldProduktid_1.setText("");
					formattedTextField.setText("");
					formattedTextField_1.setText("");
					formattedTextField_2.setText("");
					formattedTextField_3.setText("");
					formattedTextField_4.setText("");
					formattedTextField_5.setText("");
					formattedTextField_6.setText("");
					formattedTextField_7.setText("");
					formattedTextField_8.setText("");
					formattedTextField_9.setText("");
					formattedTextField_10.setText("");
					formattedTextField_11.setText("");
				}
			});
			btnRydFelter.setBounds(282, 13, 91, 23);
			panel_3.add(btnRydFelter);

			JFormattedTextField formattedTextField_14 = new JFormattedTextField();
			formattedTextField_14.setText("Produkt ID:\r\n");
			formattedTextField_14.setBounds(10, 95, 171, 20);

		}
	
		}
		
